package binary_tree.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 二叉树的中序遍历的非递归
 * @Author Stark
 * @Date 2022/2/24 10:18
 */
public class Num94_InOrderNonRecursion {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        while(!stack.isEmpty()){
            //一直往左子树走到底
            while(node != null){
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            list.add(node.val);
            //往右结点走,处理右结点
            node = node.right;
        }
        return list;
    }
}
